package com.senmiao.ssm.controller;

import com.senmiao.ssm.aspect.annotation.*;
import com.senmiao.ssm.pojo.AdminShiro;
import com.senmiao.ssm.service.CommonService;
import com.senmiao.ssm.service.SystemManagerService;
import com.senmiao.ssm.utils.Result;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统管理员操作controller
 */
@RestController
public class SystemManagerController {

    @Resource
    private SystemManagerService systemManagerService;

    @Resource
    private CommonService commonService;

    /**
     * 创建管理员账号
     * @param account
     * @param username
     * @param phone
     * @param email
     * @param roleId
     * @return
     */
    @RequiresPermissions("/admin/account/create:post")
    @DoInsertLog(tableName = "tb_admin")
    @RequestMapping("/admin/account/create")
    public Result createAdmin(String account, String username, String phone, String email,@RequestParam("roleId") List<Integer> roleId){
        AdminShiro principal = (AdminShiro) SecurityUtils.getSubject().getPrincipal();
        return systemManagerService.createAdmin(account,username,phone,email,roleId);
    }

    /**
     * 获取管理员账号信息
     * @param adminId
     * @return
     */
    @RequiresPermissions("/admin/accountInfo/get:get")
    @RequestMapping("/admin/accountInfo/get")
    public Result getAccountInfo(Integer adminId){
        return commonService.getAdminInfo(adminId);
    }

    /**
     * 修改管理员可用状态
     * @param adminId
     * @param disable
     * @return
     */
    @RequiresPermissions("/admin/account/disable:post")
    @RequestMapping("/admin/account/disable")
    public Result disableAccount(Integer adminId,Integer disable){
        return systemManagerService.updateAdminDisable(adminId,disable);
    }

    /**
     * 删除管理员账号
     * @param adminId
     * @return
     */
    @RequiresPermissions("/admin/account/delete:post")
    @RequestMapping("/admin/account/delete")
    public Result deleteAdmin(Integer adminId){
        return systemManagerService.deleteAdmin(adminId);
    }

    /**
     * 获取管理员账号列表，分页
     * @param pageNum
     * @param limit
     * @return
     */
    @RequiresPermissions("/admin/account/getList:get")
    @RequestMapping("/admin/account/getList")
    public Result listAdmin(Integer pageNum,Integer limit){
        return systemManagerService.listAdmin(pageNum,limit);
    }

    /**
     * 获取所有角色
     * @return
     */
    @RequiresPermissions("/admin/role/getAll:get")
    @RequestMapping("/admin/role/getAll")
    public Result getAllRole(){
        return systemManagerService.getAllRole();
    }

    /**
     * 修改管理员信息，包括角色
     * @param adminId
     * @param username
     * @param phone
     * @param email
     * @param roleIds
     * @return
     */
    @RequiresPermissions("/admin/info/change:post")
    @DoUpdateAdminRoleLog(tableName="tb_admin_role")
    @RequestMapping("/admin/info/change")
    public Result changeAdmin(Integer adminId,String username,String phone,String email,@RequestParam("roleId")List<Integer> roleIds){
        return systemManagerService.updateAdminRole(adminId, username, phone, email,roleIds);
    }

    /**
     * 重置管理员密码为账号
     * @param adminId
     * @return
     */
    @RequiresPermissions("/admin/password/reset:post")
    @RequestMapping("/admin/password/reset")
    public Result resetPassword(Integer adminId){
        return systemManagerService.resetAdminPassword(adminId);
    }

    /**
     * 获取角色列表，分页
     * @param pageNum
     * @param limit
     * @return
     */
    @RequiresPermissions("/admin/role/getList:get")
    @RequestMapping("/admin/role/getList")
    public Result listRole(Integer pageNum,Integer limit){
        return systemManagerService.listRole(pageNum,limit);
    }

    /**
     * 更新角色描述信息
     * @return
     */
    @RequiresPermissions("/admin/role/update:post")
    @DoUpdateEmployeeLog(tableName = "tb_role")
    @RequestMapping("/admin/role/update")
    public Result updateRoleComment(Integer roleId,String comment){
        return systemManagerService.updateRoleComment(roleId,comment);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @RequiresPermissions("/admin/role/delete:post")
    @DoDeleteLog(tableName = "tb_role")
    @RequestMapping("/admin/role/delete")
    public Result deleteRole(@RequestParam("roleId") List<Integer> roleIds){
        return systemManagerService.deleteRole(roleIds);
    }

    /**
     * 新增角色
     * @param roleName
     * @param comment
     * @return
     */
    @RequiresPermissions("/admin/role/create:post")
    @DoInsertLog(tableName = "tb_role")
    @RequestMapping("/admin/role/create")
    public Result createRole(String roleName,String comment){
        return systemManagerService.createRole(roleName,comment);
    }

    /**
     * 获取权限列表，分页
     * @param pageNum
     * @param limit
     * @return
     */
    @RequiresPermissions("/admin/permission/getList:get")
    @RequestMapping("/admin/permission/getList")
    public Result listPermission(Integer pageNum,Integer limit){
        return systemManagerService.listPermission(pageNum,limit);
    }

    /**
     * 获得所有权限
     * @return
     */
    @RequiresPermissions("/admin/permission/getAll:get")
    @RequestMapping("/admin/permission/getAll")
    public Result getAllPermission(){
        return systemManagerService.getAllPermission();
    }

    /**
     * 获取登录、登出操作日志列表
     * @param pageNum
     * @param limit
     * @return
     */
    @RequiresPermissions("/admin/log/loginAndLogout/getList:get")
    @DoQueryLog(tableName = "tb_operate_log")
    @RequestMapping("/admin/log/loginAndLogout/getList")
    public Result listLoginAndLogoutLog(Integer pageNum,Integer limit){
        return systemManagerService.listLoginAndLogoutLog(pageNum,limit);
    }
}